# -*- coding: utf-8 -*-
# @Time: 2021/9/7 22:34
# @Author: yh
# @Remark: 执行任务
import logging

from mxsoftpy import DB
from mxsoftpy.exception import DBError


class TaskDB(DB):

    def __init__(self, task_code: str):
        super().__init__()
        self.task_code = task_code

    def videos(self) -> list:
        """
        获取此任务中所包含的视频源
        """
        try:
            videos = self.tree.open('VideoQuality', 'InspectionTask\\%s' % self.task_code, file='IOT').fetchone_value(
                'videos')
        except DBError:
            logging.error('执行任务时：获取该任务所包含的视频源失败，任务code: %s' % self.task_code)
            return list()

        return videos.split(';')

    def video_test_items(self) -> dict:
        """
        获取此任务所包含的检测项目
        """
        from db.common.VideoManage.VideoQuality.conf import round_test_items
        try:
            self.tree.open('VideoQuality', 'InspectionTask\\%s\\TestItems' % self.task_code, file='IOT')
        except DBError:
            logging.error('执行任务时：打开该任务所包含的检测项目失败，任务code: %s' % self.task_code)
            return dict()
        test_items = round_test_items(self.tree.sub_items())

        return test_items

    def create_record(self, data: dict) -> None:
        """
        插入数据到巡检记录表
        :param data: 一条任务的所有巡检记录
        """
        self.table.open('MXSE')
        for video_data in data.values():
            for i in video_data:
                self.table.insert('VideoInspectionRecord', i)
